window.ROOT = "";
(function() {
	
	//初始化增删改操作权限
	/*var isAccess=$("#userRoleInfo").text()=="巡检大队"?true:false;
	if(!isAccess){
		$("#inspectAddBtn").parent().empty();	
	}*/
	//初始化分页数据
	var serverData = {};
	var serverTable;
	jQuery(document).ready(function() {
	serverTable = jQuery('#dataTable').DataTable({
			"serverSide" : true,
			"ordering" : false,
			"ajax" : {
				"dataType" : "json",
				"type" : "get",
				"cache" : false,
				"url" : window.rootPath
						+ '/ui/bussinessInspection/page',
				"data" : function(d) {
					return $.extend({}, d,
							serverData);
				}
			},
			columns : [
					{
						data : null,
						defaultContent : ""
					},
					 {
	                    data: null,
	                    defaultContent: "",
	                    render: function (data, type, full, meta) {
	                        var operate ='<button type="button" class="btn btn-sm btn-info searchBtn" title="查看详情"><i class="fa fa-search"></i></button>';
	       
	                        operate+= '<button type="button" class="btn btn-sm btn-info delBtn" title="删除"><i class="fa fa-remove"></i></button>';
						
	                        return operate;

	                    }
	                },
					{
						data : 'businesssellerBaseInfo.shopName'
					},
					{
						data : 'businesssellerBaseInfo.shopAdress'
					},
				
					{
						data : 'businesssellerBaseInfo.managementRange'
					},
					
					{
						data : 'businesssellerBaseInfo.managementPerson.name'
					},
					{
						data : 'inspectCondition.title'
					},
					{
						data : 'user.name'
					},
					{
						data : 'user.contactType'
					},

					{
						data : 'inspectDate',
						render: function (data, type, full, meta) {
	                        if (data)
	                            return moment(data).format('YYYY-MM-DD HH:mm');
	                        else
	                            return '';
	                    }
					},
					{
						data : 'remark'
					},
					/*{
						data : null,
						defaultContent : "",
						render : function(data,type, full,meta) {
							var operate="";
							if(isAccess)
								operate= '<button type="button" class="btn btn-sm btn-info editBtn" title="编辑"><i class="fa fa-pencil"></i></button>'
									+ '<button type="button" class="btn btn-sm btn-info delBtn" title="删除"><i class="fa fa-remove"></i></button>';
							
							return operate;

						}
					}*/ ],
				"drawCallback" : function(oSettings) {
				var k = 0;
				var total = oSettings.aiDisplay.length+ oSettings._iDisplayStart;

				for (var i = oSettings._iDisplayStart, iLen = total; i < iLen; i++) {
					$('td:eq(0)',oSettings.aoData[oSettings.aiDisplay[k]].nTr).text(i + 1);
					k++;
				}

                serverTable.off('click', 'tr');
                serverTable.on('click', 'tr', function (event) {
	                    event = event || window.event;
	                    var target = event.target || event.scrElement;
	                    var data = serverTable.row(this).data();
	                    if (target.className.indexOf('fa-search') != -1 || target.className.indexOf('searchBtn') != -1) {
	                        showDetail(data);
	                    }
	                    if (target.className.indexOf('fa-remove') != -1 || target.className.indexOf('delBtn') != -1) {
	                    	deleteInspection(data);
	                    }
	                   

	                    return;
                });
			}
		});
	$("#exportToExcel").click(function(){
		$("#test").attr("src","");
		var key=$("#inspectSearch").val();
		var url = rootPath+"/ui/bussinessInspection/exportToExcel?key="+key;
		$("#test").attr("src",url);
		
		/*
		$.ajax({
			url:rootPath+"/ui/bussinessInspection/exportToExcel",
			data:key,
			dataType:'json',
			type:'get',
			success:function(result){
				
			}
		});
		*/
	});
		//查看巡检详情
		var showDetail=function(data){
	     	if(!data) return;
	     	
	     	var picUrl = data.pictureAddr;
	 		if(picUrl==undefined||picUrl==""){
	 			picUrl=window.rootPath + "/img/nopicture.png";
			}else{
				picUrl=window.rootPath+'/'+picUrl;
			}
	     	
	     	jQuery('#shopName').html(data.businesssellerBaseInfo.shopName);
	     	jQuery('#inspectCondition').html(data.inspectCondition.title);
	     	jQuery('#managementPerson').html(data.businesssellerBaseInfo.managementPerson.name);
	     	jQuery('#userName').html(data.user.name);
	     	jQuery('#userContactType').html(data.user.contactType);
	     	jQuery('#inspectDate').html(moment(data.inspectDate).format('YYYY-MM-DD HH:mm'));
	     	jQuery('#shopAdress').html(data.businesssellerBaseInfo.shopAdress);
	     	jQuery('#managementRange').html(data.businesssellerBaseInfo.managementRange);
	     	jQuery('#remark').html(data.remark);
	     	jQuery('#avatar').attr("src",picUrl);
			var details = data.details;
			$("#details tr:not(:first)").empty();
			$.each(details, function (i) {
				var picAddr = this.picAddr;
		 		if(picAddr==undefined||picAddr==""){
		 			picAddr=window.rootPath + "/img/nopicture.png";
				}else{
					picAddr=window.rootPath + '/' + picAddr;
				}
				var detailsTab = $("#details");
				var html = '<tr><td>'+(i+1)+'</td><td><img src="'+picAddr+'" width="50px" height="50px"/></td>' + '<td>' + this.remark + '</td><td>'+this.inspectOpt.title + '</td></tr>';
				detailsTab.append(html);
				
			})
	     	jQuery('#checkModal').modal('show');
	     }
	
		//删除该条巡检,使用的是restful风格的DELETE提交方式
		function deleteInspection(data){
			
			if(confirm('确定删除该条巡检记录?')){
				$.ajax({
					url:window.rootPath+'/ui/bussinessInspection/delete/'+data.id,
					dataType:'json',
					type:'post',
					data:'_method=DELETE',
					success:function(result){
						//返回成功
						if(result.code==1){
							window.location=window.rootPath+'/ui/bussinessInspection';
						}
					}
				});
			}
		};
	
		//RESTFUL风格的GET方式去修改页面
		/*function editInspection(data){
			if(confirm('确定修改该条巡检记录?')){
				//修改页面
				window.location=window.rootPath+'/ui/bussinessInspection/saveOrUpdate/'+data.id;
			}
		};*/
		
		//条件检索
		$(".searchBtn").click(function(){
			serverData.key = jQuery("#inspectSearch").val();
			serverTable.draw();
		});
		//撤销查询
	    jQuery('.resetBtn').on('click',function(){
	        jQuery('#inspectSearch').val('');
	        serverData.key='';
	        serverTable.draw();
	    });
		//添加数据
		/*$("#inspectAddBtn").click(function(){
			//添加页面,使用get方式不带Id
			window.location=window.rootPath+"/ui/bussinessInspection/saveOrUpdate";
		});*/

});})();